<?php
/**
 * initialization session
 */
session_start();
session_regenerate_id();
session_cache_expire("600");

/**
 * load ./config/config.php file
 */
try {
	if(file_exists("./config/config.php")){
		require_once './config/config.php';
	}
	else {
		throw new Exception("File '<strong>./config/config.php</strong>' not found!");
	}
}
catch(Exception $e){
	echo $e->getMessage();
}

/**
 * __autoload()
 * atomatically load class
 * 
 * @param class name $className
 * @throws Exception
 */
function __autoload($className){
	
	try {
		if(file_exists("./core/controllers/autoloader.php")){
			require_once './core/controllers/autoloader.php';
			if(class_exists("Core_Controller_Autoloader")){
				$autoloder = new Core_Controller_Autoloader();
				$this->autoloader->loadFile($className);
			}
			else {
				throw new Exception("Class '<strong>Core_Router_Autoloader does not exists!");
			}
		}
		else {
			throw new Exception("File '<strong>./core/controllers/autoloader.php' does not exits!");
		}
	}
	catch(Exception $e){
		echo $e->getMessage();
	}
}

$request = $_SERVER['QUERY_STRING'];
$postVars = array();
$postVars = $_POST;
try {
	if(file_exists("./core/controllers/controllerloader.php")){
		require_once './core/controllers/controllerloader.php';
		if(class_exists("Core_Controller_Controllerloader")){
			$controllerLoader = new Core_Controller_Controllerloader();
			$controllerLoader->loadController($request, $postVars);
		}
		else {
			throw new Exception("Class '<strong>Core_Controllers_Controlerloader</strong>' does not exists!");
		}
	}
	else {
		throw new Exception("File '<strong>./core/controllers/controllerloader.php</strong>' does not exists!");
	}
}
catch(Exception $e){
	echo $e->getMessage();
}